# # -*- coding:utf-8 -*-
# import socket  # socket模块
# import threading
# import time
# import json
# # HOST = '127.0.0.1'
# HOST = '139.196.189.46'
# PORT = 8080
# END = 'This is the end marker'.encode('utf-8')

# sockets = []


# def handleSocket(client, addr):
#     while True:
#         try:
#             data = client.recv(8192000)
#             if not data:
#                 pass
#             else:
#                 for i in range(len(sockets)):
#                     try:
#                         if sockets[i] == client:
#                             pass
#                         else:
#                             print('----------------------------------------------------------------------')
#                             print(data)
#                             num = sockets[i].send(data)
#                             print(num)

#                     except:
#                         sockets[i].close()
#                         # sockets.remove(sock)
#                         continue
#         except:
#             break
#     client.close()
#     sockets.remove(client)


# s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)  # 定义socket类型，网络通信，TCP
# s.bind((HOST, PORT))  # 套接字绑定的IP与端口
# s.listen(100)  # 开始TCP监听
# while 1:
#     client, addr = s.accept()  # 接受TCP连接，并返回新的套接字与IP地址
#     print('Connected by', addr)  # 输出客户端的IP地址
#     thread = threading.Thread(target=handleSocket, args=(client, addr))
#     thread.start()
#     sockets.append(client)

# s.close()


# 创建SocketServerTCP服务器：
# import socketserver
# from socketserver import StreamRequestHandler as SRH
# from time import ctime

# host = '127.0.0.1'
# port = 9999
# addr = (host,port)
# class Servers(SRH):
#     def __init__(self):
#         self.users = []

#     def handle(self):
#         print ('got connection from ',self.client_address)
#         self.users.append()
#         while True:
#             data = self.request.recv(1024)
#             self.client_address
#             if not data:
#                 break
#             print (data)
#             print ("RECV from ", self.client_address[0])
#             self.request.send(data)
# print ('server is running....')
# server = socketserver.ThreadingTCPServer(addr,Servers)
# server.serve_forever()


import socketserver
import re
import socket


class PythonChatServer(socketserver.ThreadingTCPServer):

    def __init__(self, server_address, RequestHandlerClass):
        socketserver.ThreadingTCPServer.__init__(
            self, server_address, RequestHandlerClass)
        self.users = []


class RequestHandler(socketserver.StreamRequestHandler):

    def handle(self):
        permitAddr = ['222.92.184.74','139.196.189.46']
        print('got connection from ', self.client_address)
        if self.client_address[0] not in permitAddr:
            self.finish()
        if self not in self.server.users:
            self.server.users.append(self.request)
        
        print(self.server.users)
        while True:
            data = self.request.recv(8196000)
            if not data:
                break
            for x in self.server.users:
                try:
                    if self.request != x:
                        print(data)
                        x.send(data)
                except:
                    self.server.users.remove(x)

if __name__ == '__main__':
    hostname = '139.196.189.46'
    port = 8080
    PythonChatServer((hostname, port), RequestHandler).serve_forever()
